$(function(){
    //tab切换高度设置
//	var $height=document.documentElement.clientHeight-$(".heightBox").height();
//	$(".mui-control-content").height($height);
	
	var $height=document.documentElement.clientHeight-$("#sliderSegmentedControl").height();
	$(".mui-slider-group").height($height-1);
	
    //滚动条
	mui('.mui-scroll-wrapper').scroll({
		deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
	});
	//婚姻状况弹窗
	var marriage = new mui.PopPicker();
	marriage.setData([{
	    value: "0",
	    text: "未知"
	},{
	    value: "1",
	    text: "已婚"
	}, {
	    value: "2",
	    text: "未婚"
	}, {
	    value: "3",
	    text: "离异"
	}, {
	    value: "4",
	    text: "丧偶"
	}])
	$("#marriage").click(function(){
		marriage.show(function(item) {
			$("#marriage").text(item[0].text).css("color","#333");
			$("input[name='ismarriage']").val(item[0].value);
		})
	});
	//过敏史弹窗
	var allergy = new mui.PopPicker();
	allergy.setData([{
	    value: "0",
	    text: "否"
	}, {
	    value: "1",
	    text: "是"
	}])
	$("#allergy").click(function(){
		allergy.show(function(item) {
			$("#allergy").text(item[0].text).css("color","#333");
			$("input[name='isdrugallergy']").val(item[0].value);
			if(item[0].text=="是"){
				$("#allergyRemarks").show();
				$("input[name='allergydrugs']").removeAttr("disabled");
			}else{
				$("#allergyRemarks").hide();
				$("input[name='allergydrugs']").attr("disabled",true);
			}
		})
	});
	if($("#allergy").val()=="是"){
		$("#allergyRemarks").show();
	}else{
		$("#allergyRemarks").hide();
	}
	document.getElementById('followUp').addEventListener('tap',function(){
		var archivesId= utilModule.getQueryString("archivesid");
		var param = {"archivesid":archivesId,"currentPage":1,"pageSize":999};
		electronicArchivesMdule.sendpatientvisitlist(param,function(data){
			console.log(data);
			var info=data.pageModel.list;
			if(info!=""){
				var inforId = info[0].infoid;
				if(inforId){
					location.href="interview-record.html?infoid="+inforId+"&archivesid="+archivesId;
				}	
			}else{
				mui.openWindow({
					url:'interview-record.html?archivesid='+archivesId,
		    		id:'followUp',
				});
			}
		})
	
	});
	
	initqueryElectronicArchivesList();
	initsendpatientvisitlist();
	inituserinfo();
	
	//获取个人资料
	function inituserinfo(){
		var avatar = utilModule.getStorage("avatar");
		var param = {"userid":USERID};
		function callBack(data){
			console.log(data);
			var avatar = data.avatar;
			utilModule.setStorage("avatar", avatar);
		}
		$(".wx-picture").attr('src',avatar);
		electronicArchivesMdule.queryUserInfo(param,callBack);
	}
	
	//获取随访记录
	function initsendpatientvisitlist(){
		var archivesId= utilModule.getQueryString("archivesid");
		var param = {"archivesid":archivesId,"currentPage":1,"pageSize":999};
		function callBack(data){
			console.log(data);
			var info=data.pageModel.list;
			if(info!=null){
				var a = ["门诊","家庭访问","电话","失访","死亡"];
				var co = ["bg_blue","bg_blue","bg_blue","bg_orange","bg_dkgray"];
				var html = "";
				var $lenght=info.length+1;
				for (one in info) {
					$lenght--;
					html+= '<li infoid="'+info[one].infoid+'">'+
							'<span class="font1 '+co[info[one].visitType-1]+'">'+a[info[one].visitType-1]+'</span>'+
							'<div class="number font5"><font>第'+parseInt($lenght)+'次随访记录</font></div>';
					if(info[one].isSignature==0){
						html+= '<div class="time font2">随访日期：'+info[one].visitDate+'<font>审核中</font></div></li>';
					}else{
						html+= '<div class="time font2">随访日期：'+info[one].visitDate+'<font>审核通过</font></div></li>';
					}
				}
				$(".isit-list").append(html);
			}
		}
		electronicArchivesMdule.sendpatientvisitlist(param,callBack);
	}
	
	/*获取电子档案*/
	function initqueryElectronicArchivesList(){
		var archivesId= utilModule.getQueryString("archivesid");
		var param = {"archivesId":archivesId};
		function callBack(data){
			console.log(data);
			var a =["男","女"];
			var c  = marriage.pickers[0].items;
			var d  = allergy.pickers[0].items;
			//$(".wx-picture").val();
			$("#patName").html(data.name);
			$("#patinetInfor").html(a[data.sex-1]+"　"+data.age+"岁");
			$(".cm").val(data.height);
			$(".kg").val(data.weight);
			$("#marriage").text(c[data.ismarriage].text).css("color","#333");
			$("#allergy").text(d[data.isdrugallergy].text).css("color","#333");
			if(data.isdrugallergy !=0){
				$("#allergyRemarks").show();
				$("input[name='allergydrugs']").val(data.allergydrugs);
			}else{
				$("input[name='allergydrugs']").attr("disabled",true);
			}
			$("input[name='isdrugallergy']").val(data.isdrugallergy);
			$("input[name='ismarriage']").val(data.ismarriage);
			$(".jfjl").attr("data-id",data.id);
			$(".intoinfo").attr("href",$(".intoinfo").attr("href")+"?id="+data.id);
			$(".archivesid").attr("data-ourself",data.ourself);
		}
		electronicArchivesMdule.archives(param,callBack);
	};
	$(".html-loding").hide();
		//提交选填资料
		$("#nextBtn").on("click",function(){
		// 身高 
		var Height = $(".cm").val();
		if(Height!=""){
			 if(!(/^[1-9]\d{1,2}$/.test(Height))){ 
		    	mui.alert("您的身高有误,请重新输入","提示");  
		        return; 
		   }	
		}
		//体重 
		var Weight = $(".kg").val();
		if(Weight!=""){
			 if(!(/^[1-9]\d{1,2}$/.test(Weight))){ 
		    	mui.alert("您的体重有误,请重新输入","提示");  
		        return; 
		   }	
		}
		var id = utilModule.getQueryString("archivesid");
		var ourself = $("#followUp").data("ourself");
		
		var param =$('#userInfo').serialize()+"&type=2&patientid="+USERID+"&id="+id+"&ourself="+ourself;
		electronicArchivesMdule.updateElectronicArchives(param,callBack);
		
		function callBack(data){
			console.log(data);
			if(data.status==200||data ==1){
				mui.toast("保存成功"); 
			}else{
				mui.toast("保存失败");
			}
		}
	});
	
	$(".isit-list li").on("click",function(){
//		var archivesId= utilModule.getQueryString("archivesid");
//		var infoid=$(this).attr("infoid");
//		var VType=$(this).attr("VType");
//		if(VType==21){
//			location.href ="personalInfor-form.html?archivesid="+archivesId;
//		}else if(VType==22){
//			location.href ="supplementary-detail.html?archivesid="+archivesId;
//		}else{
//			location.href ="case-detail.html?infoid="+infoid;
//		}
		var infoid=$(this).attr("infoid");
		location.href ="case-detail.html?infoid="+infoid;
	});
	
	$("#userTableInfo").on("click",function(){
		var archivesId= utilModule.getQueryString("archivesid");
		var params = {"archivesid":archivesId};
		electronicArchivesMdule.personalinforForm(params,function(callBack){
	    	console.log(callBack);
	    	if(callBack){
	    		var infor = callBack.patientBasicInfo;
	    		if(utilModule.checkUndefined(infor)!="") {
					location.href ="personalInfor-form.html?archivesid="+archivesId;
				}else{
					mui.alert("还未有医生给您添加个人信息表","提示");
				}
			}
    	})
	});
	$("#userTableSupplement").on("click",function(){
		var archivesId= utilModule.getQueryString("archivesid");
		var params = {"archivesid":archivesId};
		electronicArchivesMdule.severePatientTable(params,function(data){
	    	console.log(data);
	    	if(data){
	    		var json = data.patientSupplementInfo;
	    		if(utilModule.checkUndefined(json)!="") {
					location.href ="supplementary-detail.html?archivesid="+archivesId;
				}else{
					mui.alert("还未添加严重患者补充表","提示");
				}
			}
    	})
	});
	
	$("#medicalHistory").on("click",function(){
		var archivesId= utilModule.getQueryString("archivesid");
		var params = {"archivesid":archivesId};
		location.href ="medical-history.html?archivesId="+archivesId;
	});
	
	//查询   就诊记录
	$("#diagnosisRecord").on("click",function(){
		var archivesId= utilModule.getQueryString("archivesid");
		location.href = "diagnostic-logout.html?archivesid="+archivesId;
	});
	
});
